home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DJLSR106.ARJ / TIMER.CC < prev    next >
C/C++ Source or Header  |  1992-03-24  |  3KB  |  133 lines

  1. /* 
  2. Copyright (C) 1990 Free Software Foundation
  3.     written by Doug Lea (dl@rocky.oswego.edu)
  4.  
  5. This file is part of the GNU C++ Library.  This library is free
  6. software; you can redistribute it and/or modify it under the terms of
  7. the GNU Library General Public License as published by the Free
  8. Software Foundation; either version 2 of the License, or (at your
  9. option) any later version.  This library is distributed in the hope
  10. that it will be useful, but WITHOUT ANY WARRANTY; without even the
  11. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  12. PURPOSE.  See the GNU Library General Public License for more details.
  13. You should have received a copy of the GNU Library General Public
  14. License along with this library; if not, write to the Free Software
  15. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  16. */
  17.  
  18. #ifdef __GNUG__
  19. #pragma implementation
  20. #endif
  21. #include <builtin.h>
  22.  
  23. // Timing functions from Doug Schmidt...
  24.  
  25. /* no such thing as "negative time"! */
  26. #define  TIMER_ERROR_VALUE -1.0   
  27.  
  28. // If this does not work on your system, change this to #if 0, and 
  29. // report the problem
  30.  
  31. #if 1
  32.  
  33. #include <sys/types.h>
  34. #if defined(USG)
  35. extern "C" {
  36. #include <sys/param.h>
  37. #include <sys/times.h>
  38. }
  39. #else
  40. #include <osfcn.h>
  41. #endif
  42.  
  43. #if defined(USG)
  44. static struct tms Old_Time;
  45. static struct tms New_Time;
  46. #else
  47. static struct rusage Old_Time;
  48. static struct rusage New_Time;
  49. #endif
  50. static int    Timer_Set = 0;
  51.  
  52. double start_timer()
  53. {
  54.    Timer_Set = 1;
  55. #if defined(USG)
  56.    times(&Old_Time);
  57.    return((double) Old_Time.tms_utime / HZ);
  58. #else
  59.    getrusage(RUSAGE_SELF,&Old_Time);        /* set starting process time */
  60.    return(Old_Time.ru_utime.tv_sec + (Old_Time.ru_utime.tv_usec / 1000000.0));
  61. #endif
  62. }
  63.  
  64. /* Returns process time since Last_Time.
  65.    If parameter is 0.0, returns time since the Old_Time was set.
  66.    Returns TIMER_ERROR_VALUE if `start_timer' is not called first.  */
  67.  
  68. double return_elapsed_time(double Last_Time)
  69. {
  70.    if (!Timer_Set) {
  71.       return(TIMER_ERROR_VALUE);
  72.    }   
  73.    else {
  74.     /* get process time */
  75. #if defined(USG)
  76.       times(&New_Time);
  77. #else
  78.       getrusage(RUSAGE_SELF,&New_Time);
  79. #endif
  80.       if (Last_Time == 0.0) {
  81. #if defined(USG)
  82.      return((double) (New_Time.tms_utime - Old_Time.tms_utime) / HZ);
  83. #else
  84.          return((New_Time.ru_utime.tv_sec - Old_Time.ru_utime.tv_sec) + 
  85.                ((New_Time.ru_utime.tv_usec - Old_Time.ru_utime.tv_usec) 
  86.                 / 1000000.0));
  87. #endif
  88.       }
  89.       else {
  90. #if defined(USG)
  91.      return((double) New_Time.tms_utime / HZ - Last_Time);
  92. #else
  93.          return((New_Time.ru_utime.tv_sec + 
  94.                 (New_Time.ru_utime.tv_usec / 1000000.0)) - Last_Time);
  95. #endif
  96.       }
  97.    }
  98. }
  99.  
  100. #ifdef VMS
  101. void sys$gettim(unsigned int*) asm("sys$gettim");
  102.  
  103. getrusage(int dummy,struct rusage* time){
  104.     double rtime;
  105.     unsigned int systime[2];
  106.     int i;
  107.     sys$gettim(&systime[0]);
  108.     rtime=systime[1];
  109.     for(i=0;i<4;i++) rtime *= 256;
  110.     rtime+= systime[0];
  111. /* we subtract an offset to make sure that the number fits in a long int*/
  112.     rtime=rtime/1.0e+7-4.144e+9;
  113.     time->ru_utime.tv_sec= rtime;
  114.     rtime=(rtime-time->ru_utime.tv_sec)*1.0e6;    
  115.     time->ru_utime.tv_usec= rtime;
  116. }
  117. #endif
  118. #else /* dummy them out */
  119.  
  120. double start_timer()
  121. {
  122.   return TIMER_ERROR_VALUE;
  123. }
  124.  
  125. double return_elapsed_time(double)
  126. {
  127.   return TIMER_ERROR_VALUE;
  128. }
  129.  
  130. #endif /* timing stuff */
  131.  
  132.  
  133.